App.controller('markController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {

	$scope.getGridName = function(parentId){
		 return 'markGrid_' + parentId;
	}

    //查询顶层分类
    Page.ajaxPostQuiet('markType/queryTopType.do', null, function (resp) {
        $scope.parentMarks = resp.data;
        $(resp.data).each(function(index, parent) {
        	var gridName = $scope.getGridName(parent.markId);
	       	$scope[gridName] = angular.extend({}, $scope.markGrid);
	       	$scope[gridName].gName = gridName;
	   	});
    });
    
    $scope.markGrid = {
    	options : {
    		paging: false,
    		bAutoWidth:false
		},
        column_defs: [
            {
                target: 2,
                buttons: [
                    {
                        name: '删除',
                        clazz: 'btn btn-delete',
                        icon: 'fa-trash-o',
                        event: 'remove'
                    }
                ]
            }
        ],
        add : function(mark) {
            $scope.markType = {};
            $scope.markType.parentId = mark.markId;
            $scope.markModal.show();
        },
        remove : function(row) {
            Page.confirm("确定要删除吗？", function () {
                Page.ajaxPost('markType/delete.do', row, function () {
                	var gridName = $scope.getGridName(row.parentId);
                    $scope[gridName].query();
                });
            });
        }
    };
    
    
    
    $scope.markModal = {
		onShow : function() {
			if($scope.markType.markId) {
				this.setTitle("编辑标签");
			} else {
				this.setTitle("新增标签");
			}
		},
    	onOkClick : function() {
	    	if(!$scope.validateForm.$valid) {
            	$scope.validateForm.doValidate();
	        } else {
	            if(!$scope.markType.markId) { //添加
	                Page.ajaxPost('markType/add.do', $scope.markType, function (resp) {
	                    var gridName = $scope.getGridName($scope.markType.parentId);
                    	$scope[gridName].query();
	                    $scope.markModal.hide();
	                });
	            } else {
	                Page.ajaxPost('markType/update.do', $scope.markType, function (resp) {
	                    var gridName = $scope.getGridName($scope.markType.parentId);
                    	$scope[gridName].query();
	                    $scope.markModal.hide();
	                });
	            }
	        }
    	},
    	onHidden : function() {
        	 $scope.validateForm.reset();
    	}
    };
    


}]);
